package cn.jarlen.jwt.tools.antivirus;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import cn.jarlen.jwt.tools.menu.IMenuProcess;

/**
 * @author jarlen
 * Create by 2024/7/2 10:50
 */
public class AntivirusCheck implements IMenuProcess {

    @Override
    public void onExecute(Context context) {

        Log.w("AntivirusCheck", "漏洞检测 开始");

        String securityPatchVer = Build.VERSION.SECURITY_PATCH;
        String osVer = android.os.Build.VERSION.RELEASE;

        List<CVELib> antivirusList = AntivirusDao.getInstance(context).queryAllCVE("Android");
        List<CVELib> checkResult = checkAntivirus(antivirusList, securityPatchVer, osVer);
        Log.w("AntivirusCheck", "漏洞检测结果：" + JSON.toJSONString(checkResult));
    }

    private List<CVELib> checkAntivirus(List<CVELib> antivirusList, String securityPatchVer, String osVer) {
        if (antivirusList == null || antivirusList.isEmpty()) {
            return null;
        }

        List<CVELib> checkAntRes = new ArrayList<>();
        long securityPatch2Time = CVEHelper.dateToStamp(securityPatchVer, "yyyy-MM-dd");
        int count = antivirusList.size();
        for (int index = 0; index < count; index++) {
            CVELib antivirus = antivirusList.get(index);
            String patchVer = antivirus.getPatchVer();
            if (checkAffectedVersion(osVer, antivirus.getAffectedVersion())
                    && !existSecurityPatch(securityPatch2Time, patchVer)) {
                checkAntRes.add(antivirus);
            }
        }
        return checkAntRes;
    }

    private boolean existSecurityPatch(long currentPatchTime, String antivirusPatch) {
        long antivirusPatchTime = CVEHelper.dateToStamp(antivirusPatch, "yyyy-MM-dd");
        return currentPatchTime >= antivirusPatchTime;
    }

    private boolean checkAffectedVersion(String currentSysVer, String affectedVersions) {
        String[] affectedVersionArray;
        if (affectedVersions.contains("、")) {
            affectedVersionArray = affectedVersions.split("、");
        } else {
            affectedVersionArray = affectedVersions.split(",");
        }
        int size = affectedVersionArray.length;
        for (int index = 0; index < size; index++) {
            return isAffectedVersion(currentSysVer, affectedVersionArray[index]);
        }
        return false;
    }

    private boolean isAffectedVersion(String currentSysVer, String affectedVersion) {
        currentSysVer = currentSysVer.trim();
        affectedVersion = affectedVersion.trim();
        affectedVersion = affectedVersion.startsWith(".") ? affectedVersion.substring(1) : affectedVersion;
        if (affectedVersion.startsWith("≤")) {
            affectedVersion = affectedVersion.substring(1);
            int compareVer = CVEHelper.compareVer(currentSysVer, affectedVersion);
            return compareVer == 2 || compareVer == 1;
        } else if (affectedVersion.startsWith("<")) {
            affectedVersion = affectedVersion.substring(1);
            int compareVer = CVEHelper.compareVer(currentSysVer, affectedVersion);
            return compareVer == 2;
        } else {
            int compareVer = CVEHelper.compareVer(currentSysVer, affectedVersion);
            return compareVer == 1;
        }
    }

}
